<?php
/**
* Django's templating engine (partial) PHP implementation
* provides some filters support
* @link http://djangoproject.com Django project
* this file is from a webappkit package (kit)
* @link http://webappkit.net
* @package djtpl
* @author J.Ducastel <jeremie@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
* Adds the arg to the value.
* @param mixed $value
* @param mixed $arg
* @return mixed
* /
function djtplFilter_add($value,$arg=null) {

	return $value;
}/**/

/**
* Adds slashes. Useful for passing strings to JavaScript, for example
* @param mixed $value
* @param mixed $arg
* @return mixed
* /
function djtplFilter_addslashes($value,$arg=null) {

	return $value;
}/**/

/**
* Capitalizes the first character of the value.
* @param mixed $value
* @param mixed $arg
* @return mixed
* /
function djtplFilter_capFirst($value,$arg=null) {

	return $value;
}/**/

/**
* Centers the value in a field of a given width.
* @param mixed $value
* @param mixed $arg
* @return mixed
* /
function djtplFilter_center($value,$arg=null) {

	return $value;
}/**/

/**
* Removes all values of arg from the given string.
* @param mixed $value
* @param mixed $arg
* @return mixed
* /
function djtplFilter_cut($value,$arg=null) {

	return $value;
}/**/

/**
* Formats a date according to the given format (same as the now tag).
* @param mixed $value
* @param mixed $arg
* @return string
*/
function djtplFilter_date($value,$arg=null) {
	return date($arg,$value);
}/**/

/**
* If value is unavailable, use given default.
* @param mixed $value
* @param mixed $arg
* @return mixed
*/
function djtplFilter_default($value,$arg) {
	return $value?$value:$arg;
}/**/

/**
* Returns true if the value is divisible by the argument.
* @param mixed $value
* @param mixed $arg
* @return mixed
* /
function djtplFilter_divisibleBy($value,$arg=null) {

	return $value;
}/**/

/**
* Escapes a string's HTML.
* Specifically, it makes these replacements:
* "&" to "&amp;"
* < to "&lt;"
* > to "&gt;"
* '"' (double quote) to '&quot;'
* "'" (single quote) to '&#39;'
* @param mixed $value
* @param mixed $arg
* @return mixed
*/
function djtplFilter_escape($value,$arg=null) {
	$value=str_replace('&','&amp;',$value);
	$value=str_replace('<','&lt;',$value);
	$value=str_replace('>','&gt;',$value);
	$value=str_replace('"','&quot;',$value);
	$value=str_replace('\'','&#39;',$value);
	return $value;
}/**/

/**
* Format the value like a 'human-readable' file size (i.e. '13 KB', '4.1 MB', '102 bytes', etc).
* @param mixed $value
* @param mixed $arg
* @return mixed
*/
function djtplFilter_filesize($value,$arg='bytes,Kb,Mb,Gb,Tb') {
	$value=floatval($value);
	$unitLabels=explode(',',$arg);
	$unit=0;
	//echo $value;
	while ($value>1024 and isset($unitLabels[$unit+1])) {
		$unit++;
		$value=$value/1024;
	}
	$unitLabel=$unitLabels[$unit];
	if (is_int($value))
		$size=$value;
	else
		$size=sprintf('%.2f', $value);
	return $size.' '.$unitLabel;
}/**/

/**
* Returns the first item in a list.
* @param mixed $value
* @param mixed $arg
* @return mixed
* /
function djtplFilter_first($value,$arg=null) {

	return $value;
}/**/

/**
* Replaces ampersands with &amp; entities.
* @param mixed $value
* @param mixed $arg
* @return mixed
*/
function djtplFilter_fix_ampersands($value) {
	return str_replace('&','&amp;',$value);
}/**/

/**
* Rounds a floating-point number to one decimal place -- but only if there's a decimal part to be displayed. For example:
* 36.123 gets converted to 36.1
* 36.15 gets converted to 36.2
* 36 gets converted to 36
* @param mixed $value
* @param mixed $arg
* @return mixed
* /
function djtplFilter_floatFormat($value,$arg=null) {

	return $value;
}/**/

/**
* Given a whole number, returns the requested digit of it, where 1 is the right-most digit, 2 is the second-right-most digit, etc. Returns the original value for invalid input (if input or argument is not an integer, or if argument is less than 1). Otherwise, output is always an integer.
* @param mixed $value
* @param mixed $arg
* @return mixed
* /
function djtplFilter_get_digit($value,$arg=null) {

	return $value;
}/**/

/**
* Joins a list with a string, like Python's str.join(list).
* @param array $value
* @param string $arg glue
* @return string
*/
function djtplFilter_join($value,$arg=';') {
	if (is_array($value))
		$value=join($arg,$value);
	return $value;
}/**/

/**
* Returns the length of the value. Useful for lists.
* @param string|array $value
* @return int
*/
function djtplFilter_length($value) {
	if (is_string($value))
		return strlen($value);
	else if (is_array($value))
		return count($value);
	return $value;
}/**/

/**
* Returns a boolean of whether the value's length is the argument.
* @param mixed $value
* @param mixed $arg
* @return mixed
* /
function djtplFilter_length_is($value,$arg=null) {

	return $value;
}/**/

/**
* Converts newlines into <p> and <br /> tags.
* @param mixed $value
* @param mixed $arg
* @return mixed
* /
function djtplFilter_linebreaks($value,$arg=null) {

	return $value;
}/**/

/**
* Displays text with line numbers.
* @param mixed $value
* @param mixed $arg
* @return mixed
* /
function djtplFilter_linenumbers($value,$arg=null) {

	return $value;
}/**/

/**
* Left-aligns the value in a field of a given width.
* @param mixed $value
* @param mixed $arg
* @return mixed
* /
function djtplFilter_ljust($value,$arg=null) {

	return $value;
}/**/

/**
* Converts a string into all lowercase.
* @param mixed $value
* @param mixed $arg
* @return mixed
*/
function djtplFilter_lower($value,$arg=null) {
	return strtolower($value);
}/**/

/**
* Returns the value turned into a list. For an integer, it's a list of digits. For a string, it's a list of characters.
* @param mixed $value
* @param mixed $arg
* @return mixed
* /
function djtplFilter_make_list($value,$arg=null) {

	return $value;
}/**/

/**
* Returns a plural suffix if the value is not 1. By default, this suffix is 's'.
* For words that require a suffix other than 's', you can provide an alternate suffix as a parameter to the filter.
* For words that don't pluralize by simple suffix, you can specify both a singular and plural suffix, separated by a comma.
* @param mixed $value
* @param mixed $arg
* @return mixed
*/
function djtplFilter_pluralize($value,$arg='s') {
	if (!is_int($value))
		$value=intval($value);
	$args=explode(',',$arg);
	switch (true) {
		case $value<=1 and count($args)>1: // provided singular suffix
			return $args[0];
		case $value<=1: // singular, no suffix
			return '';
		case $value>1: // plural, last arg is the plural form
			return end($args);
		default: // somethign must be wrong
			return '?';
	}
	// return $value;
}/**/

/**
* Returns a random item from the list.
* @param mixed $value
* @param mixed $arg comma-separated list
* @return mixed
* /
function djtplFilter_random($value,$arg=null) {

	return $value;
}/**/

/**
* Removes a space separated list of [X]HTML tags from the output.
* @param mixed $value
* @param string $arg tags to remove
* @return string
*/
function djtplFilter_removetags($value,$arg=null) {
	$forbidden=explode(' ',$arg);
	static $all=array(
		'html','head','body','span'
		,'title','link','meta','style'
		,'p','h1','h2','h3','h4','h5','h6','strong','em','abbr','acronym','address','bdo','blockquote','cite','q','code','ins','del','dfn','kbd','pre','samp','var','br'
		,'a','base','img','area','map','object','param'
		,'ul','ol','li','dl','dt','dd','table','tr','td','th','tbody','thead','tfoot','col','colgroup','caption'
		,'form','input','textarea','select','option','optgroup','button','label','fieldset','legend','script','noscript');
	// building allowed list
	$allowed='';
	foreach ($all as $tag)
		if (!in_array($tag,$forbidden))
			$allowed.="<$tag>";
	//echo htmlentities($allowed);
	return strip_tags($value,$allowed);
}/**/

/**
* Right-aligns the value in a field of a given width.
* @param mixed $value
* @param mixed $arg
* @return mixed
* /
function djtplFilter_rjust($value,$arg=null) {

	return $value;
}/**/

/**
* Returns a slice of the list.
* Uses the same syntax as Python's list slicing. See http://diveintopython.org/native_data_types/lists.html#odbchelper.list.slice for an introduction.
* @param mixed $value
* @param mixed $arg
* @return mixed
* /
function djtplFilter_slice($value,$arg=null) {

	return $value;
}/**/

/**
* Converts to lowercase, removes non-word characters (alphanumerics and underscores) and converts spaces to hyphens. Also strips leading and trailing whitespace.
* @param mixed $value
* @param mixed $arg
* @return mixed
*/
function djtplFilter_slugify($value,$arg=null) {
	$value=strtolower($value);
	$value=ereg_replace('[^a-z0-9_ ]','',$value);
	$value=trim($value);
	$value=str_replace(' ','-',$value);
	return $value;
}/**/

/**
* Formats the variable according to the argument, a string formatting specifier.
* @param mixed $value
* @param mixed $arg
* @return mixed
* /
function djtplFilter_stringFormat($value,$arg=null) {

	return $value;
}/**/

/**
* Strips all [X]HTML tags.
* @param mixed $value
* @param mixed $arg
* @return mixed
*/
function djtplFilter_striptags($value,$arg=null) {
	return strip_tags($value);
}/**/

/**
* Formats a time according to the given format (same as the now tag).
* @param mixed $value
* @param mixed $arg
* @return mixed
* /
function djtplFilter_time($value,$arg=null) {

	return $value;
}/**/

/**
* Formats a date as the time since that date (i.e. "4 days, 6 hours").
* @param mixed $value
* @param mixed $arg
* @return mixed
* /
function djtplFilter_timesince($value,$arg=null) {

	return $value;
}/**/

/**
* Similar to timesince, except that it measures the time from now until the given date or datetime. For example, if today is 1 June 2006 and conference_date is a date instance holding 29 June 2006, then {{ conference_date|timeuntil }} will return "28 days".
* @param mixed $value
* @param mixed $arg
* @return mixed
* /
function djtplFilter_timeuntil($value,$arg=null) {

	return $value;
}/**/

/**
* Truncates a string after a certain number of words.
* @param string $value
* @param int $arg
* @return string
*/
function djtplFilter_truncatewords($value,$arg=50) {
	$words=explode(' ',$value);
	$qt=intval($arg);
	$words=array_slice($words,0,$qt);
	return join(' ',$words);
}/**/

/**
* Converts a string into all uppercase.
* @param mixed $value
* @return mixed
*/
function djtplFilter_upper($value) {
	return strtoupper($value);
}/**/

/**
* Escapes a value for use in a URL.
* @param mixed $value
* @return mixed
*/
function djtplFilter_urlEncode($value) {
	return urlencode($value);
}/**/

/**
* Converts URLs in plain text into clickable links.
* @param mixed $value
* @param mixed $arg
* @return mixed
*/
function djtplFilter_urlize($value,$arg=null) {

	return ereg_replace('([^"])(http://)([a-zA-Z0-9./_%-]+)([^"])?','\\1<a href="\\2\\3">\\3</a>\\4',$value);
}/**/

/**
* Converts URLs into clickable links, truncating URLs to the given character limit
* @param mixed $value
* @param mixed $arg
* @return mixed
* /
function djtplFilter_urlizetrunc($value,$arg=null) {

	return $value;
}/**/

/**
* Returns the number of words.
* @param mixed $value
* @param mixed $arg
* @return mixed
*/
function djtplFilter_wordCount($value) {
	$words=explode(' ',$value);
	return count($words);
}/**/

/**
* Wraps words at specified line length.
* @param mixed $value
* @param mixed $arg
* @return mixed
* /
function djtplFilter_wordwrap($value,$arg=null) {

	return $value;
}/**/

/**
* Given a string mapping values for true, false and (optionally) None, returns one of those strings according to the value:
* true	yeah,no,maybe	yeah
* False	"yeah,no,maybe"	no
* None	"yeah,no,maybe"	maybe
* None	"yeah,no"	"no" (converts None to False if no mapping for None is given)
* @param mixed $value
* @param mixed $arg
* @return mixed
*/
function djtplFilter_yesNo($value,$arg) {
	$cases=explode(',',$arg);
	switch (true) {
		case $value: // true
			return $cases[0];
		case is_null($value) and isset($cases[2]): // null with null case
			return $cases[2];
		case !isset($cases[1]): // false or null without false case
			return $cases[0];
		default: // false or null without null case
			return $cases[1];
	}
	//return $value;
}/**/

/**
* var dump
* @param mixed $value
* @param mixed $arg
* @return string
*/
function djtplFilter_dump($var,$cut=500,$niv=1) {
	//return wakdebug::vardump($var);
	if (!is_null($cut))
		$cut=intval($cut);
	// analysing var, building and returning string
	$details='';
	$type=gettype($var);
	$value='';
	$newline=chr(13);
	$indent='. ';
	for ($n=1; $n<$niv; $n++)
		$indent.='. ';
	switch (true) {
		/*case is_numeric($var):
			$value=strval($var);
			break;*/
		case is_string($var):
			//$type='string';
			$details=strlen($var);
			$value=substr($var,0,$cut);
			$value=djtplfilter_xmlentities($value);
			break;
		case is_array($var):
			//$type='array';
			$details=count($var);
			foreach ($var as $key => $item)
				$value.=$newline.$indent.$key.' = '.djtplFilter_dump($item,$cut,$niv+1);
			break;
		case is_object($var):
			$details=get_class($var);
			if ($parent=get_parent_class($var)) $details.=' extends '.$parent;
			$subvars=get_object_vars($var);
			foreach ($subvars as $key => $item) {
				// skipping vars starting by _
				if ($key[0]=='_') continue;
				$value.=$newline.$indent.$key.' = '.djtplFilter_dump($item,$cut,$niv+1);
			}
			break;
		case is_bool($var):
			//$details=
			$details=$var?'true':'false';
			$value='';
			break;
		case is_null($var):
			$details='null'; $value='null';
			break;
		default:
			//$type=gettype($var);
			$value=substr(strval($var),0,$cut);
	}
	// building and returning string
	$r="$type($details) $value";
	if ($niv==1) $r.=$newline;
	//echo $r;
	return $r;
}

function djtplFilter_vardump($value,$cut=500) {
	return djtplFilter_dump($value,$cut);
}

/**
*
* @param mixed $value
* @param mixed $arg
* @return mixed
*/
function djtplFilter_xmlEntities($value) {
	$value=str_replace('&','&amp;',$value);
	$value=str_replace('<','&lt;',$value);
	$value=str_replace('>','&gt;',$value);
	$value=str_replace('"','&quot;',$value);
	$value=str_replace('\'','&apos;',$value);
	return $value;
}/**/

/**
*
* @param mixed $value
* @param mixed $arg
* @return mixed
* /
function djtplFilter_($value,$arg=null) {

	return $value;
}/**/
?>